/*
* $Id: HousingStore.java 237 2010-12-31 00:12:54Z volok-aleksej@yandex.ru $
*
* Copyright 2008-2010 by Volokitin Alexey
* All rights reserved
*
*/
package apiary.db;

import java.io.*;

/**
 * Hive database
 * @author lexus
 */
public class HousingStore extends DBStore
{
    /**
     * Container of hive records
     * @author lexus
     */
    public class HousingRecord implements DBRecord
    {
        /** Identificator of hive */
        public int idHousing;
        /** Name hive */
        public String name;
        /** Identificator of family */
        public int idFamily;
        /** Type of hive */
        public int typeHousing;
        /** Type of frame */
        public int typeFrames;
        /** Is deleted hive */
        public boolean isDelete;

        /**
         * Constructor
         */
        public HousingRecord() {
            name = new String();
        }

        /**
         * Gets byte array of record
         * @return byte array
         */
        public byte[] getData() {
            try {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                DataOutputStream dos = new DataOutputStream(baos);
                dos.writeInt(idHousing);
                dos.writeUTF(name);
                dos.writeInt(idFamily);
                dos.writeInt(typeHousing);
                dos.writeInt(typeFrames);
                dos.writeBoolean(isDelete);
                return baos.toByteArray();
            } catch (IOException ex) {}
            return null;
        }

        /**
         * Sets byte array record.
         * Array will be parsed and all fields of it will be filled
         * @param data byte array
         * @return number read byte
         */
        public int setData(byte[] data) {
            int readData = 0;
            try {
                ByteArrayInputStream bais = new ByteArrayInputStream(data);
                DataInputStream dis = new DataInputStream(bais);
                idHousing = dis.readInt();
                name = dis.readUTF();
                idFamily = dis.readInt();
                typeHousing = dis.readInt();
                typeFrames = dis.readInt();
                isDelete = dis.readBoolean();
                readData = data.length - dis.available();
            } catch (IOException ex) {}
            return readData;
        }

    }

    /**
     * Constructor
     */
    public HousingStore() {
        super("db_housing");
    }

    /**
     * Gets new record
     * @return new record
     */
    public DBRecord getRecord() {
        return new HousingRecord();
    }

}
